﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace clock
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Init();
        }

        private void Init()
        {
            this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            this.AutoSize = false;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            int alertHour = int.Parse(tb_Hour.Text);
            int alertMinute = int.Parse(tb_Minute.Text);
            setClock set = new setClock();
            set.login(this, alertHour, alertMinute);
        }

        //定义时间类型
        public class Time
        {
            public int hour { get; set; }
            public int minute { get; set; }
        }

        //定义事件发布者闹钟
        public class Clock
        {
            public Form1 form = new Form1();
            //定义委托类型
            public delegate void Action(object sendeer, Time time, Time alertTime, Form1 form);
            //创建委托实例
            public event Action Running;
            //定义引发事件运行的方法
            public void Run(int nowHour, int nowMinute, int alarthour, int alarmminute, Form1 form)
            {
                Time nowtime = new Time();
                nowtime.hour = nowHour;
                nowtime.minute = nowMinute;

                Time alertTime = new Time();
                alertTime.hour = alarthour;
                alertTime.minute = alarmminute;

                Running(this, nowtime, alertTime, form);
            }
        }

        //定义事件订阅者类型
        public class setClock
        {
            public Clock clock = new Clock();

            private void Clock_Running(object sender, Time time, Time alertTime, Form1 form)
            {
                form.label2.Text = time.hour + ":" + time.minute;
                form.Refresh();
            }
            private void Clock_Alarming(object sender, Time time, Time alertTime, Form1 form)
            {
                if(time.hour == alertTime.hour && time.minute == alertTime.minute)
                {
                    form.label1.Text = "已经" + alertTime.hour + ":" + alertTime.minute + "了！！饮茶先啦！";
                    form.Refresh();
                }
            }
            
            //注册事件
            public void login(Form1 form, int alertHour, int alertMinute)
            {
                int hour = DateTime.Now.Hour;
                int minute = DateTime.Now.Minute;

                clock.Running += new Clock.Action(Clock_Running);
                clock.Running += new Clock.Action(Clock_Alarming);
                while (true)
                {
                    System.Threading.Thread.Sleep(1000);
                    clock.Run(hour, minute, alertHour, alertMinute, form);
                    minute++;
                    if(minute +1 == 60)
                    {
                        minute = minute - 59;
                        hour = hour + 1;
                    }
                }
            }
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }
    }
}
